﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VK.Comm;
using VK.Casemeet.Lines;
using VK.Comm.TransactSql;
using VK.Casemeet.Photos;
using System.Data;
using System.Web.Script.Serialization;
using VK.Casemeet.Merchants;

namespace VK.Casemeet.Web.ShopAdmin.Lines
{
    public partial class LineEdit : System.Web.UI.Page
    {
        public int destinationArea1, destinationArea2, destinationArea3,
            stagingArea1, stagingArea2, stagingArea3;
        public string lineFeatures, referenceStroke, costDescription, precautions;
        public string imgUrl = "Null.jpg";

        public int lineID = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            (this.Master as Site).SiteMapPathControl.MapPathList = new List<MapPath>(){
                new MapPath{ Text="桌面",Url="/ShopAdmin/Home.aspx"},
                new MapPath{ Text="活动线路列表",Url="/ShopAdmin/Lines/LineList.aspx"},
                new MapPath{ Text="编辑活动线路",Url="#"}
            };

            lineID = Util.GetQueryInt("LineID");

            if (!IsPostBack)
            {
                LineHelper.BindTypeDDL(ddlType, OptionType.Select);
                LineHelper.BindHolidayDDL(ddlHoliday, OptionType.Select);
                LineHelper.BindStayDDL(ddlStay, OptionType.Select);
                LineHelper.BindGradeDDL(ddlGrade, OptionType.Select);

                LoadData();
            }
        }

        internal void LoadData()
        {
            if (lineID != 0)
            {
                LineEntity line = LineBLL.GetLine(lineID);
                txtName.Text = line.Name;
                destinationArea1 = line.DestinationProvinceID;
                destinationArea2 = line.DestinationCityID;
                destinationArea3 = line.DestinationAreaID;
                stagingArea1 = line.StagingProvinceID;
                stagingArea2 = line.StagingCityID;
                stagingArea3 = line.StagingAreaID;
                ddlType.SelectedValue = line.Type.ToString();
                txtDays.Text = line.Days.ToString();
                ddlHoliday.SelectedValue = line.Holiday.ToString();
                ddlStay.SelectedValue = line.Stay.ToString();
                ddlGrade.SelectedValue = line.Grade.ToString();
                txtPrice.Text = Util.ViewNumber(line.Price.ToString());
                lineFeatures = line.LineFeatures;
                referenceStroke = line.ReferenceStroke;
                costDescription = line.CostDescription;
                precautions = line.Precautions;
                txtHits.Text = line.Hits.ToString();
                txtCreated.Text = Util.FormatDateTime(line.Created, DateTimeStyle.ShortDate2);
                lbIsEnable.Text = PageHelper.GetIsEnableLabel(line.IsEnable);

                IList<Condition> conditionPhotolst = new List<Condition>();
                conditionPhotolst.Add(new Condition { TableName = "Photo", FieldName = "ObjectType", ConditionType = ConditionType.NumeralEqual, ConditionValue = 1, DbType = DbType.Int32 });
                conditionPhotolst.Add(new Condition { TableName = "Photo", FieldName = "ObjectID", ConditionType = ConditionType.NumeralEqual, ConditionValue = lineID, DbType = DbType.Int32 });
                rptPhoto.DataSource = PhotoBLL.GetPhotoList(conditionPhotolst, "Created", SortMode.Asc);
                rptPhoto.DataBind();

                IList<Condition> conditionSchedulelst = new List<Condition>();
                conditionSchedulelst.Add(new Condition { TableName = "LineSchedule", FieldName = "LineID", ConditionType = ConditionType.NumeralEqual, ConditionValue = lineID, DbType = DbType.Int32 });
                rptSchedule.DataSource = ScheduleBLL.GetLineScheduleList(conditionSchedulelst, "DepartureDate", SortMode.Asc);
                rptSchedule.DataBind();
            }
        }

        protected void butSave_Click(object sender, EventArgs e)
        {
            LineEntity line = LoadEntity();
            line.LineID = lineID;
            if (line.ShopID != AdminBLL.GetSessionAdmin().ShopID) return;

            if (LineBLL.UpdateLine(line) > 0)
            {
                Message.Msg(this.Page, CS.MsgEditSuccess, MessageType.Success);
            }
            else
            {
                Message.Msg(this.Page, CS.MsgEditFailure, MessageType.Failure);
            }
            LoadData();
        }

        protected LineEntity LoadEntity()
        {
            LineEntity line = LineBLL.GetLine(lineID);
            line.Name = txtName.Text;
            line.DestinationProvinceID = Util.GetFormInt("ddlDestinationArea1");
            line.DestinationCityID = Util.GetFormInt("ddlDestinationArea2");
            line.DestinationAreaID = Util.GetFormInt("ddlDestinationArea3");
            line.StagingProvinceID = Util.GetFormInt("ddlStagingArea1");
            line.StagingCityID = Util.GetFormInt("ddlStagingArea2");
            line.StagingAreaID = Util.GetFormInt("ddlStagingArea3");
            line.Type = int.Parse(ddlType.SelectedValue);
            line.Days = int.Parse(txtDays.Text);
            line.Holiday = int.Parse(ddlHoliday.SelectedValue);
            line.Stay = int.Parse(ddlStay.SelectedValue);
            line.Grade = int.Parse(ddlGrade.SelectedValue);
            line.Price = Util.GetTextDecimal(txtPrice.Text);
            line.LineFeatures = Util.GetFormString("txtLineFeatures");
            line.ReferenceStroke = Util.GetFormString("txtReferenceStroke");
            line.CostDescription = Util.GetFormString("txtCostDescription");
            line.Precautions = Util.GetFormString("txtPrecautions");
            line.Hits = int.Parse(txtHits.Text);
            line.Created = Util.ConverDateTime(txtCreated.Text);
            string[] imgUrlArray = new JavaScriptSerializer().Deserialize<string[]>(Util.GetFormString("hidImgUrlArray"));
            line.ImgUrllst = new List<string>(imgUrlArray);
            ScheduleEntity[] scheduleArray = new JavaScriptSerializer().Deserialize<ScheduleEntity[]>(Util.GetFormString("hidScheduleArray"));
            line.Schedulelst = new List<ScheduleEntity>(scheduleArray);

            return line;
        }
    }
}